import { defHttp } from '/@/utils/http/axios';
import { isArray } from '/@/utils/is';
import { useMessage } from '/@/hooks/web/useMessage';

const { createConfirm } = useMessage();

// url: {
//   list: '/api-online/diagram/diagramConfiguration/list',
//   deleteBatch: '/api-online/diagram/diagramConfiguration/deleteBatch',
//   delete: '/api-online/diagram/diagramConfiguration/delete'
// },

//list: '/api-online/online/cgform/head/list',
// edit: 'diagram/diagramConfiguration/edit',
// add: 'diagram/diagramConfiguration/add',

export enum Api {
  onlGraphreportParamList = '/api-online/diagram/diagramConfiguration/param/listByHeadId',
  onlGraphreportItemList = '/api-online/diagram/diagramConfiguration/item/listByHeadId',
}

const list = (params) => defHttp.get({ url: '/api-online/diagram/diagramConfiguration/list', params });

const deleteOne = (params, handleSuccess) => {
  return defHttp.delete({ url: '/api-online/diagram/diagramConfiguration/delete', params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
};

const batchDelete = (params, handleSuccess) => {
  createConfirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    iconType: 'warning',
    onOk: () => {
      return defHttp.delete({ url: '/api-online/diagram/diagramConfiguration/deleteBatch', data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    },
  });
};

const saveOrUpdate = (params, isUpdate) => {
  if (isUpdate) {
    return defHttp.put({ url: '/api-online/diagram/diagramConfiguration/edit', params });
  } else {
    return defHttp.post({ url: '/api-online/diagram/diagramConfiguration/add', params });
  }
};

const getReportParam = (id) => {
  return defHttp.get({ url: '/api-online/online/cgreport/api/getParamsInfo/' + id });
};

const getDataSourceList = () => {
  return defHttp.get({ url: '/api-upms/sys/dataSource/options' });
};

const analyzeSql = (params) => {
  return defHttp.get({
    url: '/api-online/online/cgreport/head/parseSql?' + params,
  });
};

const analyzeField = (params) => {
  return defHttp.post({
    url: '/api-online/online/cgreport/head/parseField',
    params,
  });
};

const duplicateCodeCheck = (code) => {
  return defHttp.get(
    {
      url: '/api-online/diagram/diagramConfiguration/codeCheck?code=' + code,
    },
    { isTransformResponse: false }
  );
};

export const onlGraphreportParamList = Api.onlGraphreportParamList;
export const onlGraphreportItemList = Api.onlGraphreportItemList;
export { list, deleteOne, batchDelete, saveOrUpdate, getReportParam, getDataSourceList, analyzeSql, analyzeField, duplicateCodeCheck };
